package com.sevenyear.excelanalyze.listener.excel;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.sevenyear.excelanalyze.common.utils.CalculateUtils;
import com.sevenyear.excelanalyze.entity.PartitionBaseInfo;
import com.sevenyear.excelanalyze.entity.WaterSupply;
import com.sevenyear.excelanalyze.service.PartitionBaseInfoService;
import com.sevenyear.excelanalyze.service.WaterSupplyService;

/**
 * @author ：SevenYear
 * @description：TODO
 * @date ：2021/8/30 15:05
 */
public class WaterSupplyListener extends AnalysisEventListener<WaterSupply> {

    private WaterSupplyService waterSupplyService;
    private PartitionBaseInfoService partitionBaseInfoService;

    public WaterSupplyListener() {
    }

    public WaterSupplyListener(WaterSupplyService waterSupplyService, PartitionBaseInfoService partitionBaseInfoService) {
        this.waterSupplyService = waterSupplyService;
        this.partitionBaseInfoService = partitionBaseInfoService;
    }

    @Override
    public void invoke(WaterSupply waterSupply, AnalysisContext analysisContext) {
        //计算平均值
        float average = CalculateUtils.average(waterSupply.getWaterSupplyMin(), waterSupply.getWaterSupplyMax());
        waterSupply.setWaterSupplyAvg(average);
        //设置分区主键id,分区编码partitionCode,区域编码areaCode
        QueryWrapper<PartitionBaseInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("landform",waterSupply.getLandform()).eq("clay_thickness",waterSupply.getClayThickness());
        PartitionBaseInfo partitionBaseInfo = partitionBaseInfoService.getOne(wrapper);
        waterSupply.setPartitionId(partitionBaseInfo.getId());
        waterSupply.setPartitionCode(partitionBaseInfo.getPartitionCode());
        waterSupply.setAreaCode(waterSupply.getArea()+waterSupply.getPartitionCode());
        UpdateWrapper<WaterSupply> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("area_code",waterSupply.getAreaCode());
        waterSupplyService.saveOrUpdate(waterSupply,updateWrapper);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}
